package com.sunducation.wts.penbox.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sunducation.wts.penbox.R;
import com.wetao.lib_common.bean.response.ExamDetailInfo;
import com.wetao.lib_common.utils.ComUtil;

import java.util.List;
/**
 * author: zhouyh
 * created on: 3/9/21 3:52 PM
 * description: 作业记录适配器
 */
public class WorkRecordDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private static final int HEAD_ITEM = 0;
    private static final int ONE_ITEM = 1;
    private int headViewCount = 1;
    private Context mContext;
    private ExamDetailInfo.DataBean dataBean;
    private List<ExamDetailInfo.DataBean.QuestionListBean> questionListBeans;
    private int selectIndex = 0;


    public WorkRecordDetailAdapter(Context mContext, List<ExamDetailInfo.DataBean.QuestionListBean> noticeBeans) {
        this.mContext = mContext;
        this.questionListBeans = noticeBeans;
        dataBean = new ExamDetailInfo.DataBean();
    }

    public void setHeadData(ExamDetailInfo.DataBean data){
        this.dataBean = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == HEAD_ITEM){
            View head = LayoutInflater.from(mContext).inflate(R.layout.work_record_detail_head,viewGroup,false);
            return new HeadViewHolder(head);
        }else if (viewType == ONE_ITEM){
            View root = LayoutInflater.from(mContext).inflate(R.layout.work_record_detail_item,viewGroup,false);
            return new RecordDetailViewHolder(root);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int mPosition) {
        if (viewHolder instanceof HeadViewHolder){
            if (dataBean != null){
                HeadViewHolder headViewHolder = (HeadViewHolder) viewHolder;
                headViewHolder.tvScore.setText("得" + (dataBean.getScore() == null ? "0" : dataBean.getScore()) + "分/" + "共" + dataBean.getTotalScore()+"分");
            }
        }else if (viewHolder instanceof RecordDetailViewHolder){

            final int i = mPosition - 1;
            RecordDetailViewHolder recordViewHolder = (RecordDetailViewHolder) viewHolder;
            recordViewHolder.tvQuestionNo.setText(questionListBeans.get(i).getQuestionNo() + ".");

            if (questionListBeans.get(i).getMyAnswer()!=null&& !questionListBeans.get(i).getMyAnswer().equals("未识别")){
                //判断是主观题还是客观题  http是图片主观题
                if (questionListBeans.get(i).getMyAnswer().startsWith("http")||
                        questionListBeans.get(i).getMyAnswer().startsWith("https")){
                    recordViewHolder.tvQuestionAnswer.setBackgroundResource((R.drawable.ic_answering));
                    recordViewHolder.tvQuestionAnswer.setText(null);
                }else {
                    //如果为单选题
                    if (questionListBeans.get(i).getAnswers().size() == 1){
                        recordViewHolder.tvQuestionAnswer.setText(questionListBeans.get(i).getMyAnswer()+"/" +
                                questionListBeans.get(i).getAnswers().get(0));
                    }else {
                        recordViewHolder.tvQuestionAnswer.setText(questionListBeans.get(i).getMyAnswer()+"/" +
                                questionListBeans.get(i).getAnswers());
                    }
                    if (questionListBeans.get(i).getMyScore() == 0){
                        recordViewHolder.tvQuestionAnswer.setTextColor(mContext.getResources().getColor(R.color.group_red_bg));
                    }else {
                        recordViewHolder.tvQuestionAnswer.setTextColor(mContext.getResources().getColor(R.color.wifi_Text));
                    }
                    recordViewHolder.tvQuestionAnswer.setBackground(null);
                }

            }else {
                recordViewHolder.tvQuestionAnswer.setText("__");
                recordViewHolder.tvQuestionAnswer.setTextColor(mContext.getResources().getColor(R.color.wifi_Text));
                recordViewHolder.tvQuestionAnswer.setBackground(null);
            }

            recordViewHolder.tvQuestionScore.setText("(" + (questionListBeans.get(i).getMyScore() == null ? "0" :questionListBeans.get(i).getMyScore())
                    + "/" + questionListBeans.get(i).getQuestionScore() + ")");

            if (getSelectIndex() == i ){
                recordViewHolder.tvPlayTime.setTextColor(mContext.getResources().getColor(R.color.wifi_line));
                recordViewHolder.tvQuestionNo.setTextColor(mContext.getResources().getColor(R.color.wifi_line));
            }else {
                recordViewHolder.tvPlayTime.setTextColor(mContext.getResources().getColor(R.color.wifi_Text6));
                recordViewHolder.tvQuestionNo.setTextColor(mContext.getResources().getColor(R.color.wifi_Text6));
            }

            if (questionListBeans.get(i).getVoiceComment()!=null && questionListBeans.get(i).getDuration()!=null){
                recordViewHolder.ivVoice.setVisibility(View.VISIBLE);
                recordViewHolder.tvPlayTime.setVisibility(View.VISIBLE);
                recordViewHolder.tvDuration.setVisibility(View.VISIBLE);
                String durationTime = ComUtil.strToTime(questionListBeans.get(i).getDuration());
                recordViewHolder.tvDuration.setText(durationTime);

                String playerTime = questionListBeans.get(i).getPlayerTime();

                if (questionListBeans.get(i).getPlayState() == 0){
                    recordViewHolder.ivVoice.setImageResource(R.drawable.ic_video_play);
                    recordViewHolder.tvPlayTime.setText(
                            String.format("%s/","00:00"));
                }else if (questionListBeans.get(i).getPlayState() == 2) {
                    recordViewHolder.ivVoice.setImageResource(R.drawable.ic_video_play);
                    recordViewHolder.tvPlayTime.setText(String.format("%s/",(TextUtils.isEmpty(playerTime)? "00:00":playerTime)));
                }else {
                    recordViewHolder.ivVoice.setImageResource(R.drawable.ic_video_pause);
                    recordViewHolder.tvPlayTime.setText(String.format("%s/",(TextUtils.isEmpty(playerTime)? "00:00":playerTime)));
                }
            }else{
                recordViewHolder.ivVoice.setVisibility(View.GONE);
                recordViewHolder.tvPlayTime.setVisibility(View.GONE);
                recordViewHolder.tvDuration.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public int getItemCount() {
        return headViewCount + questionListBeans.size();
    }

    public boolean isHeadView(int position) {
        return headViewCount != 0 && position < headViewCount;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeadView(position)){
            return HEAD_ITEM;
        }
        return ONE_ITEM;
    }

    class HeadViewHolder extends RecyclerView.ViewHolder{

        private TextView tvScore;
        public HeadViewHolder(@NonNull View itemView) {
            super(itemView);
            tvScore = itemView.findViewById(R.id.tv_score);
        }

    }

    class RecordDetailViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rlDetailItem;
        private TextView tvQuestionNo;
        private TextView tvQuestionAnswer;
        private TextView tvQuestionScore;
        private ImageView ivVoice;
        private TextView tvPlayTime;
        private TextView tvDuration;

        public RecordDetailViewHolder(@NonNull View itemView) {
            super(itemView);

            rlDetailItem = itemView.findViewById(R.id.rl_record_detail_item);
            tvQuestionNo = itemView.findViewById(R.id.tv_question_no);
            tvQuestionAnswer = itemView.findViewById(R.id.tv_question_answer);
            tvQuestionScore = itemView.findViewById(R.id.tv_question_score);
            tvDuration = itemView.findViewById(R.id.tv_voice_time);
            ivVoice = itemView.findViewById(R.id.iv_voice);
            tvPlayTime = itemView.findViewById(R.id.tv_play_time);
        }
    }


    public void setPlayerTime(int pos, String time)
    {
        questionListBeans.get(pos).setPlayerTime(time);
        notifyDataSetChanged();
    }

    public void setPlayState(int index, int state){

        questionListBeans.get(index).setPlayState(state);
        //当录音状态为停止状态
        if (state == 0){
            questionListBeans.get(index).setPlayerTime(null);
        }
        notifyDataSetChanged();
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
        notifyDataSetChanged();
    }

    private OnRecordItemClickListener recordItemClickListener;
    public interface OnRecordItemClickListener{
        void onItemClickListener(int position);
        void onChangeClick(int position); //切换
    }

    public void setRecordItemClickListener(OnRecordItemClickListener workItemClickListener) {
        this.recordItemClickListener = workItemClickListener;
    }
}
